package it.bela.pgcdc.service.impl;

import java.util.List;

import it.bela.pgcdc.common.exception.ServiceException;
import it.bela.pgcdc.common.utils.Utilities;
import it.bela.pgcdc.dao.MailingListDao;
import it.bela.pgcdc.domain.MailingList;
import it.bela.pgcdc.service.MailingListService;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
@Service
@Transactional(rollbackFor = ServiceException.class)
public class MaililngListServiceImpl implements MailingListService {
	
	private static final Logger log = Logger.getLogger(MaililngListServiceImpl.class);
	
	@Autowired(required = true)
	private MailingListDao mailingListDao;
	
	private boolean hasValidKey(MailingList mailingList) {
		return (mailingList.getId() != 0);
	}

	@Override
	public List<MailingList> findByExample(MailingList mailingList) throws ServiceException {
		
		try {
			log.debug("Start list MailingList");
			
			mailingList = (MailingList) Utilities.setDefaultParametersSelect(mailingList);
			
			List<MailingList> list = mailingListDao.findByExample(mailingList);
			
			log.debug("End list MailingList");
			
			return list;

		} catch (Exception e) {
			log.debug("findByExample MailingList failed" + e.getMessage(), e);
			throw new ServiceException(e);
		}
	}

	public List<MailingList> list() throws ServiceException {
		
		try {
			log.debug("Start list");
			
			MailingList mailingList = new MailingList();
			
			mailingList = (MailingList) Utilities.setDefaultParametersSelect(mailingList);

			List<MailingList> list = mailingListDao.findByExample(mailingList);
			
			log.debug("End list");
			
			return list;

		} catch (Exception e) {
			log.debug("findByExample failed" + e.getMessage(), e);
			throw new ServiceException(e);
		}
	}

	public boolean delete(MailingList mailingList) throws ServiceException {
		log.debug(mailingList.getClass().toString() + " - Start delete");
		
		String message = mailingList.getClass().toString() + " - delete MailingList failed.";
		try {
			if (!hasValidKey(mailingList)) {
				String messageInner = message + " Oggetto senza id.";
				log.debug(messageInner);
				throw new ServiceException(messageInner);
			}
			
			mailingList = (MailingList) mailingListDao.findById(mailingList.getClass(), mailingList.getId());
			mailingListDao.delete(mailingList);
			
			log.debug(mailingList.getClass().toString() + " - End delete MailingList");
			return true;
		} catch (Exception e) {
			log.debug(message + e.getMessage(), e);
			throw new ServiceException(e);
		}
	}

	public boolean persist(MailingList mailingList) throws ServiceException {
		log.debug(mailingList.getClass().toString() + " - Start persist MailingList");
		
		String message = mailingList.getClass().toString() + " - persist MailingList failed.";
		try {
			
			mailingList = (MailingList) Utilities.setDefaultParametersInsert(mailingList);
			mailingListDao.persist(mailingList);
			
			log.debug(MailingList.class.toString() + " - End persist MailingList");
			return true;
		} catch (Exception e) {
			log.debug(message + e.getMessage(), e);
			throw new ServiceException(e);
		}
	}
	
	public boolean save(MailingList mailingList) throws ServiceException {
		log.debug(mailingList.getClass().toString() + " - Start save MailingList");
		
		String message = MailingList.class.toString() + " - save MailingList failed.";
		try {
			
			mailingList = (MailingList) Utilities.setDefaultParametersInsert(mailingList);
			mailingListDao.save(mailingList);
			
			log.debug(MailingList.class.toString() + " - End save MailingList");
			return true;
		} catch (Exception e) {
			log.debug(message + e.getMessage(), e);
			throw new ServiceException(e);
		}
	}
	
	public boolean merge(MailingList mailingList) throws ServiceException {
		log.debug(MailingList.class.toString() + " - Start merge MailingList");
		
		String message = MailingList.class.toString() + " - merge MailingList failed.";
		try {
			if (!hasValidKey(mailingList)) {
				String messageInner = message + " Oggetto senza id.";
				log.debug(messageInner);
				throw new ServiceException(messageInner);
			}
			
			mailingList = (MailingList) Utilities.setDefaultParametersUpdate(mailingList);
			mailingListDao.merge(mailingList);
			
			log.debug(MailingList.class.toString() + " - End merge MailingList");
			return true;
		} catch (Exception e) {
			log.debug(message + e.getMessage(), e);
			throw new ServiceException(e);
		}
	}
	
	public MailingList findById(Class<MailingList> classe, Long id)throws ServiceException {
		log.debug(MailingList.class.toString() + " - Start findById MailingList");
		
		String message = MailingList.class.toString() + " - findById MailingList failed.";
		try {
			
			MailingList mailingList = (MailingList) mailingListDao.findById(classe, id);
			
			log.debug(MailingList.class.toString() + " - End findById MailingList");
			return mailingList;
		} catch (Exception e) {
			log.debug(message + e.getMessage(), e);
			throw new ServiceException(e);
		}
	}
}
